package com.whopxx.domain.vo;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whopxx.domain.entity.Hero;
import lombok.Data;

import java.io.Serializable;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Data
public class BasePageVO <T> implements Serializable {
    private Long pageNum;
    private Long pageSize;
    private Long total;
    private List<T> datas;


    public static <T, E> BasePageVO<T> convert(Page<E> page, Function<E, T> function) {
        List<E> records = page.getRecords();
        BasePageVO vo = new BasePageVO<>();
        vo.setPageNum(page.getCurrent());
        vo.setPageSize(page.getSize());
        vo.setTotal(page.getTotal());
        if (CollectionUtil.isEmpty(records)){
            return vo;
        }
        List<T> dates = records.stream().map(function).collect(Collectors.toList());
        vo.setDatas(dates);
        return vo;
    }
}
